﻿/**
 *     XCLIKEE
 */
using System;
using System.Collections.Generic;

namespace PropertyBinding
{
    /// <summary>
    /// 属性绑定 管理器
    ///
    /// </summary>
    public class PropertyBindingMa
    {

        private static Dictionary<string, PropertyBinding> propertyMap = new Dictionary<string, PropertyBinding>();

        /// <summary>
        /// 添加绑定
        /// </summary>
        public static void AddBinding(PropertyBinding binding)
        {
            if (propertyMap.ContainsKey(binding.PropertyName))
            {
                throw new Exception("绑定属性已存在");                
            }
            else
            {
                propertyMap.Add(binding.PropertyName, binding);
            }
        }

        /// <summary>
        /// 移除属性绑定
        /// </summary>
        /// <param name="key"></param>
        public static void RemoveBinding(string key)
        {
            propertyMap.Remove(key);
        }

        /// <summary>
        /// 获得绑定的属性
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>

        public static PropertyBinding GetBinding(string key)
        {
            propertyMap.TryGetValue(key, out var value);
            return value;
        }


        /// <summary>
        /// 获得绑定的属性
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>

        public static PropertyBindingType<T> GetBinding<T>(string key)
        {
            if (propertyMap.TryGetValue(key, out var value))
            {
                return (PropertyBindingType<T>)value;
            }

            return null;
        }

        public static void Notfiy(string key)
        {
            if (propertyMap.TryGetValue(key, out var value))
                value.NotfiyValueChange();
        }
    }

}
